package com.taoyuan.hotel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.taoyuan.hotel.dao.AmusementDao;
import com.taoyuan.hotel.entity.Amusement;
import com.taoyuan.hotel.service.intf.AmusementService;
import com.taoyuan.hotel.vo.PageBean;
import com.taoyuan.hotel.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


@Service
public class AmusementServiceImpl implements AmusementService {
    @Autowired
    private AmusementDao dao;

    @Override
    public R save(Amusement amusement) {
        if (dao.add(amusement)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public PageBean all(int page, int limit) {
        PageHelper.startPage(page,limit);
        PageInfo<Amusement> pageInfo = new PageInfo<Amusement>(dao.selectAll());
        return PageBean.bean(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public R del(int id) {
        if(id>0){
            if(dao.del(id)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R update(Amusement amusement) {
        if (dao.update(amusement)>0){
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public PageBean queryPageWhere(String name, int page, int limit) {
        if(StringUtils.hasLength(name)){
            name="%"+name+"%";
        }else {
            name = null;
        }
        PageHelper.startPage(page,limit);
        PageInfo<Amusement> pageInfo = new PageInfo<Amusement>(dao.selectAll());
        return PageBean.bean(pageInfo.getTotal(),pageInfo.getList());

    }

    @Override
    public R delBatch(int[] ids) {
        if(ids!=null && ids.length>0){
            if(dao.delBatch(ids)>0){
                return R.ok();
            }
        }
        return R.fail();
    }
}
